package ${basePackage}.service.system.impl;

import ${basePackage}.core.model.PageData;
import ${basePackage}.core.model.PageWrap;
import ${basePackage}.core.utils.Utils;
import ${basePackage}.dao.system.SystemJobLogMapper;
import ${basePackage}.dao.system.dto.QuerySystemJobLogDTO;
import ${basePackage}.dao.system.model.SystemJobLog;
import ${basePackage}.service.system.SystemJobLogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class SystemJobLogServiceImpl implements SystemJobLogService {

    @Autowired
    private SystemJobLogMapper systemJobLogMapper;

    @Override
    public Integer create(SystemJobLog systemJobLog) {
        systemJobLogMapper.insert(systemJobLog);
        return systemJobLog.getId();
    }

    @Override
    public void deleteById(Integer id) {
        systemJobLogMapper.deleteById(id);
    }

    @Override
    public void delete(SystemJobLog systemJobLog) {
        UpdateWrapper<SystemJobLog> deleteWrapper = new UpdateWrapper<>(systemJobLog);
        systemJobLogMapper.delete(deleteWrapper);
    }

    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        systemJobLogMapper.deleteBatchIds(ids);
    }

    @Override
    public void updateById(SystemJobLog systemJobLog) {
        systemJobLogMapper.updateById(systemJobLog);
    }

    @Override
    public void updateByIdInBatch(List<SystemJobLog> systemJobLogs) {
        if (CollectionUtils.isEmpty(systemJobLogs)) {
            return;
        }
        for (SystemJobLog systemJobLog: systemJobLogs) {
            this.updateById(systemJobLog);
        }
    }

    @Override
    public SystemJobLog findById(Integer id) {
        return systemJobLogMapper.selectById(id);
    }

    @Override
    public SystemJobLog findOne(SystemJobLog systemJobLog) {
        QueryWrapper<SystemJobLog> wrapper = new QueryWrapper<>(systemJobLog);
        return systemJobLogMapper.selectOne(wrapper);
    }

    @Override
    public List<SystemJobLog> findList(SystemJobLog systemJobLog) {
        QueryWrapper<SystemJobLog> wrapper = new QueryWrapper<>(systemJobLog);
        return systemJobLogMapper.selectList(wrapper);
    }
  
    @Override
    public PageData<SystemJobLog> findPage(PageWrap<QuerySystemJobLogDTO> pageWrap) {
        IPage<SystemJobLog> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        QueryWrapper<SystemJobLog> queryWrapper = new QueryWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        queryWrapper.lambda()
                .eq(pageWrap.getModel().getJobId() != null, SystemJobLog::getJobId, pageWrap.getModel().getJobId())
                .eq(pageWrap.getModel().getSnippetCode() != null, SystemJobLog::getSnippetCode, pageWrap.getModel().getSnippetCode())
                .eq(pageWrap.getModel().getJobDistributeGroup() != null, SystemJobLog::getJobDistributeGroup, pageWrap.getModel().getJobDistributeGroup())
                .ge(pageWrap.getModel().getBusinessTime() != null, SystemJobLog::getBusinessTime, Utils.Date.getStart(pageWrap.getModel().getBusinessTime()))
                .le(pageWrap.getModel().getBusinessTime() != null, SystemJobLog::getBusinessTime, Utils.Date.getEnd(pageWrap.getModel().getBusinessTime()))
                .eq(pageWrap.getModel().getServerIp() != null, SystemJobLog::getServerIp, pageWrap.getModel().getServerIp())
                .eq(pageWrap.getModel().getTriggerType() != null, SystemJobLog::getTriggerType, pageWrap.getModel().getTriggerType())
                .eq(pageWrap.getModel().getStatus() != null, SystemJobLog::getStatus, pageWrap.getModel().getStatus())
                .ge(pageWrap.getModel().getCreateTime() != null, SystemJobLog::getCreateTime, Utils.Date.getStart(pageWrap.getModel().getCreateTime()))
                .le(pageWrap.getModel().getCreateTime() != null, SystemJobLog::getCreateTime, Utils.Date.getEnd(pageWrap.getModel().getCreateTime()));
        for(PageWrap.SortData sortData: pageWrap.getSorts()) {
            if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) {
                queryWrapper.orderByDesc(sortData.getProperty());
            } else {
                queryWrapper.orderByAsc(sortData.getProperty());
            }
        }
        return PageData.from(systemJobLogMapper.selectPage(page, queryWrapper));
    }

    @Override
    public long count(SystemJobLog systemJobLog) {
        QueryWrapper<SystemJobLog> wrapper = new QueryWrapper<>(systemJobLog);
        return systemJobLogMapper.selectCount(wrapper);
    }
}
